import os
import logging
import logging.config
from core.config import BASE_LOG_DIR

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(pathname)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            #'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'filename': os.path.join(BASE_LOG_DIR, 'debug.log'),
            'when': "D",
            'interval': 1,
            #'maxBytes': 1024 * 1024 * 2,
            'backupCount': 7,
            'formatter': 'verbose'
        },
        "request": {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',#'logging.FileHandler',
            'filename': os.path.join(BASE_LOG_DIR, 'request.log'),
            'formatter': 'verbose',
            'maxBytes': 1024 * 1024 * 2,
            'backupCount': 1
        },
        "server": {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',#'logging.FileHandler',
            'filename': os.path.join(BASE_LOG_DIR, 'server.log'),
            'formatter': 'verbose',
            'maxBytes': 1024 * 1024 * 2,
            'backupCount': 1
        },
        "root": {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',#'logging.FileHandler',
            'filename': os.path.join(BASE_LOG_DIR, 'root.log'),
            'formatter': 'verbose',
            'maxBytes': 1024 * 1024 * 2,
            'backupCount': 1
        },
        "db_backends": {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',#'logging.FileHandler',
            'filename': os.path.join(BASE_LOG_DIR, 'db_backends.log'),
            'formatter': 'verbose',
            'maxBytes': 1024 * 1024 * 2,
            'backupCount': 1
        },
        "autoreload": {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',#'logging.FileHandler',
            'filename': os.path.join(BASE_LOG_DIR, 'autoreload.log'),
            'formatter': 'verbose',
            'maxBytes': 1024 * 1024 * 2,
            'backupCount': 1
        }
    },
    'loggers': {
        "django": {
            "level": "DEBUG",
            "handlers": ["console", "file"],
            'propagate': False,
        },
        "django.request": {
            "level": "DEBUG",
            "handlers": ["request"],
            'propagate': False,
        },
        "django.server": {
            "level": "DEBUG",
            "handlers": ["server"],
            'propagate': False,
        },
        "django.db.backends": {
            "level": "DEBUG",
            "handlers": ["db_backends"],
            'propagate': False,
        },
        "django.utils.autoreload": {
            "level": "INFO",
            "handlers": ["autoreload"],
            'propagate': False,
        },
        'asyncio':{
            'level':'WARNING',
        }
    },
    "root": {
        "level": "DEBUG",
        "handlers": ["console","root","file"]
    }
}


if not os.path.exists(BASE_LOG_DIR):
    os.mkdir(BASE_LOG_DIR)

def get_logger(level=logging.INFO):
    # 创建logger对象
    logger = logging.getLogger()
    logger.setLevel(level)
    # 创建控制台日志处理器
    console_handler = logging.StreamHandler()
    console_handler.setLevel(level)
    # 定义颜色输出格式
    color_formatter = colorlog.ColoredFormatter(
        '%(log_color)s%(levelname)s: %(message)s',
        log_colors={
            'DEBUG': 'cyan',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'red,bg_white',
        }
    )
    # 将颜色输出格式添加到控制台日志处理器
    console_handler.setFormatter(color_formatter)
    # 移除默认的handler
    for handler in logger.handlers:
        logger.removeHandler(handler)
    # 将控制台日志处理器添加到logger对象
    logger.addHandler(console_handler)
    return logger

if not os.path.exists(BASE_LOG_DIR):
    os.mkdir(BASE_LOG_DIR)

logging.config.dictConfig(LOGGING)
sysLog= get_logger()
# logging.basicConfig(level=logging.INFO)
